|
|
|
|
|
|
|
The second part of the function uses the VarPtr function to obtain the address of the structure in Visual Basic's memory. The LenB function retrieves the size of a structure as it is stored within Visual Basic. A dump of the structure within Visual Basic appears as follows: |
|
|
|
|
|
|
|
|
In VB it contains:
12F5E8: 34 12 78 56 41 0 42 0 43 0 44 0 CC 9A 17 0 |
|
|
|
|
|
|
|
|
The only difference is the fixed-length string portion of the structure. The string appears here as the characters 41 00 42 00 43 00 44 00the same "ABCD" string except that each character is 2 bytesUnicode. |
|
|
|
|
|
|
|
|
Two facts are apparent from this demonstration: |
|
|
|
|
|
|
|
|
Fixed-length strings are stored within a structure, whereas only a 4-byte BSTR pointer is stored in the structure for dynamic strings. |
|
|
|
|
|
|
|
|
Strings within a structure are stored as Unicode within Visual Basic and as ANSI when passed to a DLL call. |
|
|
|
|
|
|
|
|
Tutorial 5, "The ByVal Keyword: The Solution to 90 Percent of All API Problems," discusses how string parameters are either Unicode or ANSI depending on whether you use the ANSI or Unicode entry point for a function. The same situation often applies to structures that contain strings. For example, the LOGFONT structure (which describes a logical font) contains a fixed-length string that holds the name of the font's typeface. If you look in the C header files (on the CD that comes with this book), you will find that there are actually two separate structure definitions, LOGFONTA and LOGFONTW. The LOGFONTA structure defines the string as an array of single byte characters, and the LOGFONTW structure defines the string as an array of wide characters. |
|
|
|
|
|
|
|
|
Don't assume that this will always be the case. Some functions always use ANSI strings, others always use Unicode characters. You'll have to refer to the documentation of the structure to know which one to use. |
|
|
|
|
|
|
|
|
Dynamic Strings inside Structures |
|
|
|
|
|
|
|
|
A dynamic string is stored in a VB structure as a 32-bit pointer to the string data. There are a number of DLL structures in Windows that contain pointers to strings. You can use a dynamic string in some cases, but you should use caution. The dynamic string pointer in the structure is a BSTR pointerit points to a block of memory that is managed by the OLE subsystem. If the DLL were to overwrite that pointer value, it could cause a memory leak or lead to a memory |
|
|
|
|
|